package com.bible.test.pad.activity.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import com.bible.fire.R;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class TitlesFragment extends Fragment {
	
	private static final String TAG = TitlesFragment.class.getSimpleName();
	
	boolean mDualPane;  
    int mCurCheckPosition = 0;
    
    private Integer[] imgeIDs = {R.drawable.weather,    
            R.drawable.flickr, R.drawable.gmail,   
            R.drawable.lovedsgn, R.drawable.miso,   
            R.drawable.music};   
    private String[] goodsNames = {"蛋糕", "礼物",    
            "邮票", "爱心", "鼠标", "音乐CD"};
    
    @Override
    public void onCreate(Bundle savedInstanceState){
    	Log.d(TAG, "create titles fragment");
    	super.onCreate(savedInstanceState);
    }
    
  
    @Override  
    public void onActivityCreated(Bundle savedInstanceState) {  
        super.onActivityCreated(savedInstanceState);  
  
    }  
  
    @Override  
    public void onSaveInstanceState(Bundle outState) {  
        super.onSaveInstanceState(outState);  
    }  
    
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    	Log.d(TAG, "create titles view");
        if (container == null) {
        	Log.d(TAG, "container is null");
            //return null;  
        }
        
        View view = inflater.inflate(R.layout.fragment_titles, container, false);
        ListView listView = (ListView)view.findViewById(R.id.list_goods );
        
        ArrayList<HashMap<String, Object>> goodList = new ArrayList<HashMap<String, Object>>();
        for( int i = 0; i < goodsNames.length; i++ ){
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("imageItem", imgeIDs[i]);               //图片资源   
            map.put("titleItem", "物品名称：");              //物品标题   
            map.put("infoItem", goodsNames[i]);     //物品名称    	
        	goodList.add( map );
        }
        SimpleAdapter saMenuItem = new SimpleAdapter(getActivity(), 
        		goodList, //数据源 
      		  R.layout.titles_item,   //xml实现 
      		  new String[]{"imageItem","titleItem", "infoItem"}, //对应map的Key 
      		  new int[]{R.id.imageItem,R.id.titleItem, R.id.infoItem});  //对应R的Id 

      		//添加Item到列表中 
            listView.setAdapter(saMenuItem);
      		listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					//View detailsFrame = getActivity().findViewById(R.id.detailsLayout);
					Log.d(TAG, "click detail img");
					DetailsFragment detailFragment = new DetailsFragment();
					Bundle args = new Bundle();
					args.putInt("position", position );
					detailFragment.setArguments(args);
					FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
					ft.replace(R.id.detailsLayout, detailFragment);
					ft.commit();
				}
      			
      		});
      		Log.d(TAG, "return list view");
        return view;
    }
 

}
